<?php

/**
 * Created by PhpStorm.
 * User: DELL
 * Date: 1/19/15
 * Time: 10:48 AM
 */
class GetStaplesCommand extends CConsoleCommand {
    public function run($args) {
        $url = "http://www.staples.com/Staples-Clearance/cat_BI642994";
        $dom = new simple_html_dom();
        $dom->load(HSCommon::curl($url));
        $results = array();
        if ($dom == null) return;

//        echo $dom;

        $wrappers = $dom->find('div[class="z_prod_wrapper"]');
        foreach ($wrappers as $wrapper) {
            //get id
            $div_wrapper = $wrapper->find('div', 0);
            $arr = array_filter(explode('_', $div_wrapper->attr['id']));
            end($arr);
            $key = key($arr);
            $id = trim($arr[$key]);

            //handle image
            $img_path = '';
            $img_wrapper = $wrapper->find('a[class="promoSKUImage"]', 0);
            if ($img_wrapper != null) {
                $img = $img_wrapper->find('img', 0);
                $img_path = $img == null ? "" : $img->src;
            }

            //handle name
            $name_wrapper = $wrapper->find('div[id="z_prod_Name_' . $id . '"]', 0);
            if ($name_wrapper == null) continue;
            $name = $name_wrapper->find('a', 0);
            if ($name == null) continue;

            //handle price
            $price_wrapper = $wrapper->find('p[class="z_prod_price"]', 0);
            if ($price_wrapper == null) continue;

            $deal = new Deal();
            $deal->name = trim($name->plaintext);
            $deal->url = "http://www.staples.com" . trim($name->href);
            $deal->price = trim($price_wrapper->plaintext);
            $deal->picture = trim($img_path);
            $deal->created_date = date('d-m-Y');
            $deal->type = DealTypeEnum::staples;

            $old_deal = Deal::model()->findByAttributes(array(
                'name' => $deal->name,
//                'created_date' => $deal->created_date,
                'type' => $deal->type
            ));

            if ($old_deal != null) continue;

            if ($deal->save()) {
                array_push($results, $deal);
            }
        }

        if (count($results) > 0 && count($results) > Yii::app()->params['limit']) {
            $html = '';
            $html .= '<table style="width:100%;" border="1">';
            $html .= '<tr>';
            $html .= '<th>Picture</th>';
            $html .= '<th>Name</th>';
            $html .= '<th>Price</th>';
            $html .= '<th>Link</th>';
            $html .= '</tr>';
            foreach ($results as $result) {

                $html .= '<tr>';
                if ($result->picture != '') {
                    $html .= '<td style="text-align:center"><img style="max-width: 100px; max-height: 100px" alt="" src="' . $result->picture . '"/> </td>';
                } else {
                    $html .= '<td> </td>';
                }
                $html .= '<td> ' . $result->name . '</td>';
                $html .= '<td>' . $result->price . '</td>';
                $html .= '<td><a href="' . $result->url . '">Link</a></td>';
                $html .= '</tr>';

            }
            $html .= '</table>';

            Mailer::sendEmail(Yii::app()->params['large_email'], "Staple's deals", $html);
        } else {
            foreach ($results as $k => $result) {
                if ($k > 5) break;
                $html = '';
                $html .= '<p>';
                $html .= "<div>Name: " . $result->name . "</div>";
                $html .= "<div>Price: " . $result->price . "</div>";
                $html .= "<div><a href=" . $result->url . ">Link</a></div>";
                $html .= '</p>';

                Mailer::sendEmail(Yii::app()->params['small_email'], "$result->name | $result->price", $html);
            }
        }
    }
}